package com.balabala.gym_goods.controller.admin;

import com.balabala.gym_enum.ResultEnum;
import com.balabala.gym_goods.pojo.entity.Sku;
import com.balabala.gym_goods.service.SkuService;
import com.balabala.result.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "商品详细表接口")
@RequestMapping("/admin/sku")
@CrossOrigin
public class SkuController {

    @Autowired
    private SkuService skuService;

    @ApiOperation(value = "查询所有商品详细信息")
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public Result findAll() {
        List<Sku> skuList=skuService.list();
        return Result.ok(skuList);
    }

    @ApiOperation(value = "根据id查询对应的商品详细信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result find(@PathVariable("id") Long id) {
        return  Result.ok(ResultEnum.GENERALLY_SUCCESS,skuService.getById(id));
    }


    @ApiOperation(value = "添加商品详细信息")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody Sku sku) {
        log.info("添加商品分类信息");
        sku.setCreateTime(new Date());
        sku.setUpdateTime(new Date());
        return Result.ok(skuService.save(sku));
    }

    @ApiOperation(value = "根据id删除商品详细信息")
    @RequestMapping (value = "/delete/{id}",method = RequestMethod.GET)
    public Result deleteById(@PathVariable Long id) {
        boolean result=skuService.removeById(id);
        return Result.judge(result);
    }


    @ApiOperation(value = "修改商品详细信息")
    @RequestMapping (value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody Sku sku) {
        sku.setUpdateTime(new Date());
        boolean result=skuService.updateById(sku);
        return Result.judge(result);
    }

//    @ApiOperation("根据商品分类名称模糊查询")
//    @RequestMapping (value = "/getSkuPageList",method = RequestMethod.POST)
//    public Result getSkuPageList(@RequestBody SkuQueryDto skuQueryDto){
//        PageHelper.startPage(skuQueryDto.getPage(), skuQueryDto.getLimit());
//        List<SkuQueryVo> skuQueryVoList = skuService.findName(skuQueryDto);
//        PageInfo<SkuQueryVo> pageInfo = new PageInfo<>(skuQueryVoList);
//        return Result.ok(skuQueryVoList, pageInfo.getTotal());
//    }


}
